package org.jeecg.common.i18n;

import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Locale;

@Component("localeMessageSourceService")
public class LocaleMessageSourceService {
    @Resource
    private MessageSource messageSource;
    public String getMessage(String code){
       return this.getMessage(code,new Object[]{});
    }
    public String getMessage(String code,Object[] args){
        return this.getMessage(code, args,"");
    }
    public String getMessage(String code,Object[] args,String defaultMessage){
        //这里使用比较方便的方法，不依赖request.
        Locale locale =LocaleContextHolder.getLocale();
        return this.getMessage(code, args, defaultMessage, locale);
    }
    public String getMessage(String code,Object[]args,String defaultMessage,Locale locale){
        return messageSource.getMessage(code, args, defaultMessage,locale);
    }
}
